#!/bin/bash
# validate_correctness.sh - Immediate correctness validation for masquerading vs sequential

echo "=== GPU Correctness Validation Test ==="
echo "Validating 10x performance improvement claim"
echo "Target: 128 sub-tasks × 1024×1024 matrices"
echo ""

# Build validation test
echo "1. Building validation kernels..."
cd src/cuda
nvcc -o validate_correctness validate_correctness.cu -std=c++11
cd ../..

# Run sequential mode
echo "2. Running sequential mode..."
time ./src/cuda/validate_correctness --mode sequential --size 1048576 --sub-tasks 128

# Run masquerading mode  
echo "3. Running masquerading mode..."
time ./src/cuda/validate_correctness --mode masquerading --size 1048576 --sub-tasks 128

# Compare results
echo "4. Comparing results..."
python3 -c "
import numpy as np

# Load results
sequential = np.load('output/sequential_result.npy')
masquerading = np.load('output/masquerading_result.npy')
reference = np.load('output/reference_result.npy')

# Calculate errors
seq_error = np.max(np.abs(sequential - reference))
masq_error = np.max(np.abs(masquerading - reference))

print(f'Sequential max error: {seq_error:.2e}')
print(f'Masquerading max error: {masq_error:.2e}')

if seq_error < 1e-6 and masq_error < 1e-6:
    print('✅ Both modes produce correct results')
else:
    print('❌ Results differ significantly')
    
print(f'First 5 elements - Sequential: {sequential[:5]}')
print(f'First 5 elements - Masquerading: {masquerading[:5]}')
print(f'First 5 elements - Reference: {reference[:5]}')
"

echo ""
echo "=== Validation Complete ==="